//
//  MBSpaceMyParentChildShowViewController.m
//  MiBaoTu
//
//  Created by admin on 16/9/19.
//  Copyright © 2016年 com.caesartech15. All rights reserved.
//

#import "MBSpaceMyParentChildShowViewController.h"
#import "MBSpaceMyParentChildShowPlayViewController.h"
#import "MBDetailViewController.h"

#import "MBSpaceMyParentChildShowRemoteCell.h"
#import "MBSpaceMyParentChildShowTabbarView.h"
#import "MBSpaceMyParentChildShowLocalCell.h"
#import "MBSpaceIPADMyParentChildShowRemoteCell.h"
#import "MBSpaceIPADMyParentChildShowLocalCell.h"

#import "AttentionView.h"
#import "HTTPManager.h"
#import "UpdateProgressView.h"
#import "AFNetworking.h"

#import "MBDetailViewForIpadController.h"
@interface MBSpaceMyParentChildShowViewController ()<UITableViewDataSource,UITableViewDelegate,MBSpaceMyParentChildShowTabbarViewDelegate,MBSpaceMyParentChildShowLocalCellDelegate,AttentionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegate,MBSpaceIPADMyParentChildShowLocalCellDelegate,UpdateProgressViewDelegate> {
    MBSpaceMyParentChildShowTabbarView *_tabbarView;
    UITableView                 *_tableView;
    UICollectionView            *_collectionView;
    AttentionView               *_attentionView;
    UpdateProgressView          *_updateView;
    
    int                         page;
    NSMutableArray              *_mutArr;
    NSMutableArray              *_mutTimeArr;
    NSString                    *_picUrl;
    NSString                    *_audioUrl;
     NSString                   *_capacity;
    NSDictionary                *_selectCurrentCellDic; //选中当前的cell
}



@end

@implementation MBSpaceMyParentChildShowViewController

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self onUpdateUIForView:self.orientation];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    page = 1;
    _mutArr = [[NSMutableArray alloc]init];
    _mutTimeArr = [[NSMutableArray alloc]init];
    // Do any additional setup after loading the view.
    if (self.type == MBSpaceMyParentChildShowType_TA) {
        [self setTitleView:@"TA的亲子秀"];
    } else {
        [self setTitleView:@"我的亲子秀"];
    }
    [self setBackItem];
    
    [self setRightPlayerItem];
    
    [self onCreateUI];
    
    [self onLoadNew];
}

#pragma mark - Actions
- (void)onCreateUI
{
    float heith = 0;
    if (self.type != MBSpaceMyParentChildShowType_TA) {
        _tabbarView = [[MBSpaceMyParentChildShowTabbarView alloc]initWithFrame:CGRectMake(0, (ISIPAD?NAV_HEIGHT:(ISPortrait?NAV_HEIGHT:34)), SCREEN_WIDTH, 80*PERCEN) withCount:@[@"网络内容",@"本地亲子秀"] withbgColor:[UIColor blackColor] withSelectColor:TINK_BG_COLOR];
        _tabbarView.delegate = self;
        _tabbarView.orientation = self.orientation;
        [self.view addSubview:_tabbarView];
        heith = 80*PERCEN;
    }
    
    if (ISIPAD) {
        UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc]init];
        _collectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0,(heith > 0 ? 80*PERCEN+NAV_HEIGHT : 0), SCREEN_WIDTH, self.view.height-(heith > 0 ? 80*PERCEN+NAV_HEIGHT : 0)) collectionViewLayout:flowLayout];
        _collectionView.delegate = self;
        _collectionView.dataSource = self;
        _collectionView.backgroundColor = [UIColor clearColor];
        _collectionView.backgroundView = nil;
        [_collectionView registerClass:[MBSpaceIPADMyParentChildShowRemoteCell class] forCellWithReuseIdentifier:@"MBSpaceIPADMyParentChildShowRemoteCell"];
        [_collectionView registerClass:[MBSpaceIPADMyParentChildShowLocalCell class] forCellWithReuseIdentifier:@"MBSpaceIPADMyParentChildShowLocalCell"];
        [self.view addSubview:_collectionView];
        [_collectionView addLegendHeaderWithRefreshingTarget:self refreshingAction:@selector(onLoadNew)];
        [_collectionView addLegendFooterWithRefreshingTarget:self refreshingAction:@selector(onLoadMore)];
    } else {
        float top_Y = _tabbarView.bottom;
        _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0,(heith > 0 ? top_Y : 0), SCREEN_WIDTH, self.view.height-(heith > 0 ? top_Y : 0)) style:UITableViewStylePlain];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.backgroundColor = [UIColor clearColor];
        _tableView.backgroundView = nil;
        [self.view addSubview:_tableView];
        [_tableView addLegendHeaderWithRefreshingTarget:self refreshingAction:@selector(onLoadNew)];
        [_tableView addLegendFooterWithRefreshingTarget:self refreshingAction:@selector(onLoadMore)];
    }

    _attentionView = [[AttentionView alloc]initWithTitle:@"是否要确认删除？" withCancelBt:@"确认" withSureBt:@"取消"];
    _attentionView.delegate = self;
    _attentionView.orientation = self.orientation;
}

- (void)onUpdateUIForView:(UIDeviceOrientation)orientation
{
    float heith = 0;
    if (self.type != MBSpaceMyParentChildShowType_TA) {
        _tabbarView.frame = CGRectMake(0, (ISIPAD?NAV_HEIGHT:(ISPortrait?NAV_HEIGHT:34)), SCREEN_WIDTH, 80*PERCEN);
        _tabbarView.orientation = self.orientation;
        heith = 80*PERCEN;
    }
    if (ISIPAD) {
        _collectionView.frame = CGRectMake(0,(heith > 0 ? 80*PERCEN+NAV_HEIGHT : 0), SCREEN_WIDTH, self.view.height-(heith > 0 ? 80*PERCEN+NAV_HEIGHT : 0));
        [_collectionView reloadData];
    } else {
        float top_Y = _tabbarView.bottom;
        _tableView.frame = CGRectMake(0,(heith > 0 ? top_Y : 0), SCREEN_WIDTH, SCREEN_HEIGHT-(heith > 0 ? top_Y : 0));
        [_tableView reloadData];
    }
    
    [[HTTPManager standerDefault] setOrientation:self.orientation];
}

- (void)onSelectItemForWork
{
    [self onPlayMusic];
}

#pragma mark - 加载数据
- (void)onLoadNew
{
    if (self.type == 1) {
        if (ISIPAD) {
            [_collectionView.header endRefreshing];
            [_collectionView.footer endRefreshing];
        } else {
            [_tableView.header endRefreshing];
            [_tableView.footer endRefreshing];
        }
        return;
    }
    page = 1;
    [self onloadData];
}

- (void)onLoadMore
{
    if (self.type == 1) {
        if (ISIPAD) {
            [_collectionView.header endRefreshing];
            [_collectionView.footer endRefreshing];
        } else {
            [_tableView.header endRefreshing];
            [_tableView.footer endRefreshing];
        }
        return;
    }
    ++page;
    [self onloadData];
}

- (void)onloadData
{
    UserModel *model = [UserManager standerDefult].userModel;
    if (!model) return;
    
    NSString *string = [NSString stringWithFormat:SPACE_ISSUE_LIST_URL_PATH,model.UserID,[NSNumber numberWithInt:page]];
    if (self.type == MBSpaceMyParentChildShowType_TA) {
        string = [NSString stringWithFormat:SPACE_ISSUE_LIST_URL_PATH,_taUserID,[NSNumber numberWithInt:page]];
    }
    else if (self.type == MBSpaceMyParentChildShowType_Remote) {
        string = [NSString stringWithFormat:SPACE_ISSUE_LIST_URL_PATH,model.UserID,[NSNumber numberWithInt:page]];
    }
    
    [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    [[HTTPManager standerDefault]AFGetRequest:string view:nil success:^(id json) {
        [MBProgressHUD hideHUDForView:self.view animated:YES];
        if (ISIPAD) {
            [_collectionView.header endRefreshing];
            [_collectionView.footer endRefreshing];
        } else {
            [_tableView.header endRefreshing];
            [_tableView.footer endRefreshing];
        }
        
        
        if ([json[@"ret_code"] integerValue]==0) {
            NSArray *arr = json[@"data"];
            if ([arr isKindOfClass:[NSArray class]]) {
                if (page == 1) {
                    [_mutArr removeAllObjects];
                    [_mutTimeArr removeAllObjects];
                }
                [_mutArr addObjectsFromArray:arr];
                
                [self onOptimedTime];
            }
        } 
    } failure:^(NSError *error) {
        [MBProgressHUD hideHUDForView:self.view animated:YES];
        if (ISIPAD) {
            [_collectionView.header endRefreshing];
            [_collectionView.footer endRefreshing];
        } else {
            [_tableView.header endRefreshing];
            [_tableView.footer endRefreshing];
        }
    }];
    
}

//对时间做处理
- (void)onOptimedTime
{
    if (_mutArr.count > 0) {
        //获取日期
        NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init];
        [dateFormatter setDateFormat:@"yyyy-MM-dd"];
        NSMutableArray  *timeArr = [[NSMutableArray alloc]initWithCapacity:_mutArr.count];
        for (int i=0; i<_mutArr.count; i++) {
            NSDictionary *dic = [_mutArr objectAtIndex:i];
            NSString *createTime = [dic objectForKey:@"CreateTime"];
            
            NSDate *date = [NSDate dateWithTimeIntervalSince1970:[createTime doubleValue]];
            NSString *dateStr = [dateFormatter stringFromDate:date];
            [timeArr addObject:dateStr];
        }
        
        //整合日期
        [self onSetUpTimer:0 withNextIndex:1 withArr:timeArr];
        
    }
}

- (void)onSetUpTimer:(int)selectIndex withNextIndex:(int)nextIndex withArr:(NSArray *)arr
{
    if (arr.count == 0) {
        //推出
        if (ISIPAD) {
            [_collectionView reloadData];
        } else {
            [_tableView reloadData];
        }
        return;
    }
    
    if (selectIndex == 0 && nextIndex == 1) {
        [_mutTimeArr addObject:[arr objectAtIndex:0]];
    }
    
    if (arr.count <= nextIndex) {
        //推出
        if (ISIPAD) {
            [_collectionView reloadData];
        } else {
            [_tableView reloadData];
        }
        return;
    }
    
    NSString *timer = [arr objectAtIndex:selectIndex];
    NSString *nextTimer = [arr objectAtIndex:nextIndex];
    if ([timer isEqualToString:nextTimer]) {
        [_mutTimeArr addObject:@""];
        [self onSetUpTimer:selectIndex withNextIndex:(nextIndex+1) withArr:arr];
    } else {
        [_mutTimeArr addObject:nextTimer];
        [self onSetUpTimer:nextIndex withNextIndex:(nextIndex+1) withArr:arr];
    }
}

//加载本地亲子秀
- (void)onLoadLocalData
{
    [_mutArr removeAllObjects];
    [_mutTimeArr removeAllObjects];
    
    NSArray *arr = [[FMManageObject standerDefault]onSelectLocalParentChildShowCount];
    if (arr.count == 0) {
        if (ISIPAD) {
            [_collectionView reloadData];
        } else {
            [_tableView reloadData];
        }
        return;
    } else {
        //倒序
        for (int i=(int)arr.count-1; i>=0; --i) {
            NSDictionary *dic = [arr objectAtIndex:i];
            NSString *type = [dic objectForKey:@"type"];
            if ([type hasPrefix:@"7-"]) {
                NSMutableDictionary *mDic = [NSMutableDictionary dictionaryWithDictionary:dic];
                [mDic setObject:[[type componentsSeparatedByString:@"7-"]lastObject] forKey:@"type"];
                [_mutArr addObject:mDic];
            }
        }
    }
    
    NSMutableArray  *timerArr = [[NSMutableArray alloc]initWithCapacity:_mutArr.count];
    for (NSDictionary *dic in _mutArr) {
        NSString *timer = [dic objectForKey:@"date"];
        timer = [timer componentsSeparatedByString:@" "][0];
        [timerArr addObject:timer];
    }
    
    //整合日期
    if (ISIPAD) {
//        [_mutTimeArr addObjectsFromArray:timerArr];
        [self onSetUpTimer:0 withNextIndex:1 withArr:timerArr];
    } else {
        [self onSetUpTimer:0 withNextIndex:1 withArr:timerArr];
    }
 
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - 选择器
- (void)onSelectItemForView:(MBSpaceMyParentChildShowTabbarView *)view withIndex:(NSInteger)selectIndex
{
    if (selectIndex < 2) {
        self.type = (int)selectIndex;
        if (selectIndex == 0) {
            [self onLoadNew];
        } else {
            [self onLoadLocalData];
        }
    }
}

#pragma mark - 发布、
- (void)onSelectItemForSendInformationWithValue:(NSDictionary *)value withView:(MBSpaceMyParentChildShowLocalCell *)view
{
    if (![value isKindOfClass:[NSDictionary class]]) return;
    
    _selectCurrentCellDic = value;
    
    if (!_updateView) {
        _updateView = [[UpdateProgressView alloc]init];
        _updateView.delegate = self;
        [_updateView showInView:self.view];
    }
    _updateView.orientation = self.orientation;
    
    if (_picUrl.length == 0 ) {
        [self onUpdatPicture];
    }
    else if (_picUrl.length > 0 && _audioUrl.length == 0) {
        [self onUpdateMp3WithpicUrl:_picUrl];
    }
    else if (_picUrl.length > 0 && _audioUrl.length > 0) {
        [self onUpdateRequestWithMp3Url:_picUrl withPicUrl:_audioUrl];
    }

}

//上传图片
- (void)onUpdatPicture
{
    NSString *picPath = [_selectCurrentCellDic objectForKey:@"picture"];
    if (picPath.length > 0) {
        NSString *picName = [[picPath componentsSeparatedByString:@"/"] lastObject];
        picPath = [MusicCachesDirectory stringByAppendingPathComponent:picName];
        
        UIImage *img = [UIImage imageWithContentsOfFile:picPath];;
        
        AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
        manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json",
                                                             @"text/html",
                                                             @"image/jpeg",
                                                             @"image/png",
                                                             @"application/octet-stream",
                                                             @"text/json",
                                                             nil];
        [manager POST:[NSString stringWithFormat:UPLOAD_FILE_URL_PATH,[UserManager standerDefult].userModel.UserID] parameters:nil constructingBodyWithBlock:^(id<AFMultipartFormData>  _Nonnull formData) {
            
            NSData *data = UIImagePNGRepresentation(img);
            
            NSString *dateString = [_selectCurrentCellDic objectForKey:@"date"];
            NSString *fileName = [NSString stringWithFormat:@"HeaderImg_%@.png",dateString];
            
            [formData appendPartWithFileData:data name:@"file" fileName:fileName mimeType:@"multipart/form-data"];
            
        } progress:^(NSProgress * _Nonnull uploadProgress) {
            
            CGFloat progress = 100.0 * uploadProgress.completedUnitCount / uploadProgress.totalUnitCount;
            if (progress <= 30) {
                _updateView.progress = progress;
            }
            
        } success:^(NSURLSessionDataTask * _Nonnull task, NSDictionary *  _Nullable responseObject) {
            if ([responseObject isKindOfClass:[NSDictionary class]]) {
                if ([[responseObject objectForKey:@"ret_code"]intValue] == 0) {
                    NSString *webUrl = [responseObject objectForKey:@"web_url"];
                    _picUrl = webUrl;
                    [self onUpdateMp3WithpicUrl:webUrl];
                } else {
                    [self onUpdateMp3WithpicUrl:@""];
                }
            }
        } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
            [self onUpdateMp3WithpicUrl:@""];
        }];
    } else {
        [self onUpdateMp3WithpicUrl:@""];
    }
}

//上传音频
- (void)onUpdateMp3WithpicUrl:(NSString *)picUrl
{
    NSString *mp3Path = [_selectCurrentCellDic objectForKey:@"mp3"];
    if (mp3Path.length > 0) {
        
        NSString *picName = [[mp3Path componentsSeparatedByString:@"/"] lastObject];
        mp3Path = [MusicCachesDirectory stringByAppendingPathComponent:picName];
        
        NSString *title = [_selectCurrentCellDic objectForKey:@"title"];
        
        AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
        manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json",
                                                             @"text/html",
                                                             @"image/jpeg",
                                                             @"image/png",
                                                             @"application/octet-stream",
                                                             @"text/json",
                                                             nil];
        [manager POST:[NSString stringWithFormat:UPLOAD_FILE_URL_PATH,[UserManager standerDefult].userModel.UserID] parameters:nil constructingBodyWithBlock:^(id<AFMultipartFormData>  _Nonnull formData) {
            
            
            NSData *data = [NSData dataWithContentsOfFile:mp3Path];
            [formData appendPartWithFileData:data name:@"file" fileName:[NSString stringWithFormat:@"%@.mp3",title] mimeType:@"multipart/form-data"];
            
        } progress:^(NSProgress * _Nonnull uploadProgress) {
            
            CGFloat progress = 100.0 * uploadProgress.completedUnitCount / uploadProgress.totalUnitCount;

            if (progress > 30) {
                _updateView.progress = progress;
            }
            
        } success:^(NSURLSessionDataTask * _Nonnull task, NSDictionary *  _Nullable responseObject) {
            if ([responseObject isKindOfClass:[NSDictionary class]]) {
                if ([[responseObject objectForKey:@"ret_code"]intValue] == 0) {
                    NSString *webUrl = [responseObject objectForKey:@"web_url"];
                    _audioUrl = webUrl;
                    NSString *Capacity	 = [responseObject objectForKey:@"Capacity"];
                    _capacity = [NSString stringWithFormat:@"%@",Capacity];
                    [self onUpdateRequestWithMp3Url:_audioUrl withPicUrl:picUrl];
                } else {
                    [_updateView onShowFaileContent:UpdateProgressViewType_Faile];
                }
            }
        } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
             [_updateView onShowFaileContent:UpdateProgressViewType_Faile];
        }];
    }
}

- (void)onUpdateRequestWithMp3Url:(NSString *)mp3Url withPicUrl:(NSString *)picUrl
{
    UserModel *model = [UserManager standerDefult].userModel;
    NSString *content = [_selectCurrentCellDic objectForKey:@"content"];
    NSString *type = [_selectCurrentCellDic objectForKey:@"type"];
    NSString *title = [_selectCurrentCellDic objectForKey:@"title"];
    
    [[HTTPManager standerDefault]AFGetRequest:[NSString stringWithFormat:FAMILYSHOW_ISSUESHOW_URL_PATH,model.UserID,title,picUrl,mp3Url,_capacity,content,type,@"0"] view:nil success:^(id json) {
        
        if ([json[@"ret_code"] integerValue]==0) {
            
            _picUrl = @"";
            _audioUrl = @"";
            _capacity = @"";
             [_updateView onShowFaileContent:UpdateProgressViewType_Success];
            
        }
    } failure:^(NSError *error) {
         [_updateView onShowFaileContent:UpdateProgressViewType_Faile];
    }];
    
}

- (void)onCompleteUpdateProgeressView:(UpdateProgressView *)view
{
    if (view.type == UpdateProgressViewType_Success) {
        [self removeQinZiXiuData];
    }
}

#pragma mark - 删除
- (void)onSelectItemForDelegateInformationWithValue:(NSDictionary *)value withView:(MBSpaceMyParentChildShowLocalCell *)view
{
    if (![value isKindOfClass:[NSDictionary class]]) return;
    _selectCurrentCellDic = value;
    
    if (_attentionView) {
        _attentionView.orientation = self.orientation;
        [_attentionView showInWindow];
    }
}

- (void)removeQinZiXiuData
{
    //内存
    if (_selectCurrentCellDic && [_mutArr containsObject:_selectCurrentCellDic]) {
        int index = (int)[_mutArr indexOfObject:_selectCurrentCellDic];
        [_mutArr removeObject:_selectCurrentCellDic];
        
        NSString *timer = [_mutTimeArr objectAtIndex:index];
        if (timer.length > 0 && _mutTimeArr.count > index+1) {
            NSString *nextTimer = [_mutTimeArr objectAtIndex:index+1];
            if (nextTimer.length > 0) {
                [_mutTimeArr removeObjectAtIndex:index];
            } else {
                [_mutTimeArr replaceObjectAtIndex:index+1 withObject:timer];
                [_mutTimeArr removeObjectAtIndex:index];
            }
        } else {
            [_mutTimeArr removeObjectAtIndex:index];
        }
        
        if (ISIPAD) {
            [_collectionView deleteItemsAtIndexPaths:@[[NSIndexPath indexPathForRow:index inSection:0]]];
        } else {
           
            [_tableView deleteRowsAtIndexPaths:@[[NSIndexPath indexPathForRow:index inSection:0]] withRowAnimation:UITableViewRowAnimationFade];
            if (_mutArr.count > 1) {
                [_tableView reloadRowsAtIndexPaths:@[[NSIndexPath indexPathForRow:index inSection:0]] withRowAnimation:UITableViewRowAnimationFade];
            }
        }
    }
    
    //沙河删除
    NSFileManager *fileManager = [NSFileManager defaultManager];
    NSString *mp3Path = [_selectCurrentCellDic objectForKey:@"mp3"];
    if (mp3Path.length > 0) {
        NSString *picName = [[mp3Path componentsSeparatedByString:@"/"] lastObject];
        mp3Path = [MusicCachesDirectory stringByAppendingPathComponent:picName];
        
        if ([fileManager fileExistsAtPath:mp3Path]) {
            [fileManager removeItemAtPath:mp3Path error:nil];
        }
    }
    
    NSString *picPath = [_selectCurrentCellDic objectForKey:@"picture"];
    if (picPath.length > 0) {
        NSString *picName = [[picPath componentsSeparatedByString:@"/"] lastObject];
        picPath = [MusicCachesDirectory stringByAppendingPathComponent:picName];
        
        if ([fileManager fileExistsAtPath:picPath]) {
            [fileManager removeItemAtPath:picPath error:nil];
        }
    }
    
    //数据库删除
    NSString *idStr = [_selectCurrentCellDic objectForKey:@"id"];
    [[FMManageObject standerDefault]deleteByLocalParentChildShowFilID:idStr];

}

- (void)onSelectItemForIPADSendInformationWithValue:(id)value withView:(MBSpaceIPADMyParentChildShowLocalCell *)view
{
    HTTPManager *manager = [HTTPManager standerDefault];
    manager.orientation = self.orientation;
    [manager onUpdateMusicWithView:self.view];
}
- (void)onSelectItemForIPADDelegateInformationWithValue:(id)value withView:(MBSpaceIPADMyParentChildShowLocalCell *)view
{
    if (_attentionView) {
        _attentionView.orientation = self.orientation;
        [_attentionView showInWindow];
    }
}

#pragma mark - 提示框
- (void)onSelectSureItem:(AttentionView *)view
{
    
}

- (void)onSelectCancelItem:(AttentionView *)view
{
    //删除操作
    [self removeQinZiXiuData];
}

#pragma mark - UITableViewDelegate
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (self.type == MBSpaceMyParentChildShowType_Remote || self.type == MBSpaceMyParentChildShowType_TA) {
        return _mutArr.count;
    }
    else if (self.type == MBSpaceMyParentChildShowType_Local) {
        return _mutArr.count;
    }
    return 0;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (self.type == MBSpaceMyParentChildShowType_Remote || self.type == MBSpaceMyParentChildShowType_TA) {
        static NSString *cellID = @"MyParentChildShow_RemoteCell";
        MBSpaceMyParentChildShowRemoteCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
        if (!cell) {
            cell = [[MBSpaceMyParentChildShowRemoteCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
        }
        
        if (_mutArr.count > indexPath.row) {
            cell.dataDic = [_mutArr objectAtIndex:indexPath.row];
            if (_mutTimeArr.count > indexPath.row) {
                cell.timeShow = [_mutTimeArr objectAtIndex:indexPath.row];
            }
        }
        
        return cell;
    }
    else if (self.type == MBSpaceMyParentChildShowType_Local) {
        static NSString *cellID = @"MyParentChildShow_LocalCell";
        MBSpaceMyParentChildShowLocalCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
        if (!cell) {
            cell = [[MBSpaceMyParentChildShowLocalCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
            cell.delegate = self;
        }
        
        if (_mutArr.count > indexPath.row) {
            cell.dataDic = [_mutArr objectAtIndex:indexPath.row];
            if (_mutTimeArr.count > indexPath.row) {
                cell.timeShow = [_mutTimeArr objectAtIndex:indexPath.row];
            }
        }

        return cell;
    }
    return nil;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 575*PERCEN;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (self.type == MBSpaceMyParentChildShowType_Remote) {
        UserManager *manager = [UserManager standerDefult];
        
        NSString *idStr ;
        if (manager.audioPlayerType != AudioPLayerType_Qinzix) {
            //初次进入
            idStr = @"";
            manager.audioPlayerType = AudioPLayerType_Qinzix;
        } else {
            //2次进入
            NSInteger selectIndex = manager.selectMusicIndex;
            NSArray *arr = manager.musicArr;
            Music *dics = [arr objectAtIndex:selectIndex];
            idStr = [NSString stringWithFormat:@"%@",dics.ID];
        }
        [manager setSelectMusicIndex:indexPath.row];
        [manager.musicArr removeAllObjects];
        [manager.musicArr addObjectsFromArray:[manager onTurnArrayFormQingZiXiuArray:_mutArr]];
        manager.selectMusicIndex = indexPath.row;
        [manager.qinzixiuArr removeAllObjects];
        [manager.qinzixiuArr addObjectsFromArray:_mutArr];
        
        if (ISIPAD) {
            MBDetailViewForIpadController*childShowVC=[[MBDetailViewForIpadController alloc]init];
            childShowVC.dataAllArr = _mutArr;
            childShowVC.selectMusicID = idStr;
            [self.navigationController pushViewController:childShowVC animated:YES];
            return;
            
        }
        
        MBDetailViewController *childShowVC = [[MBDetailViewController alloc] init];
        [childShowVC.dataAllArr addObjectsFromArray:_mutArr];
        childShowVC.selectMusicID = idStr;
        [self.navigationController pushViewController:childShowVC animated:YES];
    }
}

#pragma mark - IPAD UICollectionViewDelegate
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
   return _mutArr.count;
}


- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    if (self.type == MBSpaceMyParentChildShowType_Remote || self.type == MBSpaceMyParentChildShowType_TA) {
        MBSpaceIPADMyParentChildShowRemoteCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"MBSpaceIPADMyParentChildShowRemoteCell" forIndexPath:indexPath];
        cell.orientation = self.orientation;
        
        if (_mutArr.count > indexPath.row) {
            cell.dataDic = [_mutArr objectAtIndex:indexPath.row];
            if (_mutTimeArr.count > indexPath.row) {
                cell.timeShow = [_mutTimeArr objectAtIndex:indexPath.row];
            }
        }
        
        return cell;
    }
    else if (self.type == MBSpaceMyParentChildShowType_Local) {
        NSLog(@"12241414");
        MBSpaceIPADMyParentChildShowLocalCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"MBSpaceIPADMyParentChildShowLocalCell" forIndexPath:indexPath];
        cell.orientation = self.orientation;
        cell.delegate = self;
        
        if (_mutArr.count > indexPath.row) {
            cell.dataDic = [_mutArr objectAtIndex:indexPath.row];
            if (_mutTimeArr.count > indexPath.row) {
                cell.timeShow = [_mutTimeArr objectAtIndex:indexPath.row];
            }
        }
        
        return cell;
    }
    return nil;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    float itemWidth = (SCREEN_WIDTH-20*PERCEN*3)/2.;
    float itemHeight = 694*PERCEN*(ISPortrait?1.0*SCREEN_WIDTH/SCREEN_HEIGHT:1.);
    return CGSizeMake(itemWidth, itemHeight);
}
- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
    return UIEdgeInsetsMake(10*PERCEN, 20*PERCEN, 0, 20*PERCEN);
}
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section
{
    return 20*PERCEN;
}
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section
{
    return 20*PERCEN;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    if (self.type == MBSpaceMyParentChildShowType_Remote) {
        UserManager *manager = [UserManager standerDefult];
        
        NSString *idStr ;
        if (manager.audioPlayerType != AudioPLayerType_Qinzix) {
            //初次进入
            idStr = @"";
            manager.audioPlayerType = AudioPLayerType_Qinzix;
        } else {
            //2次进入
            NSInteger selectIndex = manager.selectMusicIndex;
            NSArray *arr = manager.musicArr;
            Music *dics = [arr objectAtIndex:selectIndex];
            idStr = [NSString stringWithFormat:@"%@",dics.ID];
        }
        [manager setSelectMusicIndex:indexPath.row];
        [manager.musicArr removeAllObjects];
        [manager.musicArr addObjectsFromArray:[manager onTurnArrayFormQingZiXiuArray:_mutArr]];
        manager.selectMusicIndex = indexPath.row;
        [manager.qinzixiuArr removeAllObjects];
        [manager.qinzixiuArr addObjectsFromArray:_mutArr];
        
        if (ISIPAD) {
            MBDetailViewForIpadController*childShowVC=[[MBDetailViewForIpadController alloc]init];
            childShowVC.dataAllArr = _mutArr;
            childShowVC.selectMusicID = idStr;
            [self.navigationController pushViewController:childShowVC animated:YES];
            return;
            
        }
        MBDetailViewController *childShowVC = [[MBDetailViewController alloc] init];
        [childShowVC.dataAllArr addObjectsFromArray:_mutArr];
        childShowVC.selectMusicID = idStr;
        [self.navigationController pushViewController:childShowVC animated:YES];
    }
}

@end
